/**
 * 
 */
package com.roc.page;

import java.util.ArrayList;
import java.util.List;

import com.roc.util.Constance;



/**
 * 
 * @author Administrator
 *
 */
public class BaseWhereCondition implements IWhereCondition {
	protected boolean needConverEmpty2Null = true;
	protected StringBuffer SQLWhereBuffer = new StringBuffer();
	private List<Object> params = new ArrayList<Object>();

	/**
	 * @param params the params to set
	 */
	public void setParams(List<Object> params) {
		this.params = params;
	}
	public List<Object> getParams(){
		return this.params;
	}

	public StringBuffer getWhereCondition() throws Exception {
		return new StringBuffer( SQLWhereBuffer.toString() );
	}



	public void addCondition(String fieldsName, Object fieldsValue,
			String operation, boolean NullSensitive) {
		// null condition 
		if (nullProcessed(fieldsName, fieldsValue, NullSensitive))
			return;

		// not null
		
		SQLWhereBuffer.append(" AND ").append(fieldsName).append(" ").append(operation ).append( " ?");
		
		params.add(fieldsValue);
	}

	

	public void addConditionEqual(String fieldsName, Object fieldsValue,
			boolean NullSensitive) {
		addCondition( fieldsName, fieldsValue,"=", NullSensitive);

	}

	

	public void addConditionLikeAll(String fieldsName, Object fieldsValue,
			boolean NullSensitive) {
		// null condition 
		if (nullProcessed(fieldsName, fieldsValue, NullSensitive))
			return;

		SQLWhereBuffer
			.append(" AND ")
			.append(fieldsName)
			.append(" LIKE ")
			.append("'%")
			.append(Constance.filterHQL((String)fieldsValue))
			.append("%'");
	}

	
	
	public void addConditionLikeEnd(String fieldsName, Object fieldsValue,
			boolean NullSensitive) {
		// null condition 
		if (nullProcessed(fieldsName, fieldsValue, NullSensitive))
			return;

		SQLWhereBuffer
			.append(" AND ")
			.append(fieldsName)
			.append(" LIKE ")
			.append("'%")
			.append(Constance.filterHQL((String)fieldsValue))
			.append("'");

	}

	

	public void addConditionLikeFirst(String fieldsName, Object fieldsValue,
			boolean NullSensitive) {
		// null condition 
		if (nullProcessed(fieldsName, fieldsValue, NullSensitive))
			return;

		SQLWhereBuffer
			.append(" AND ")
			.append(fieldsName)
			.append(" LIKE ")
			.append("'")
			.append(Constance.filterHQL((String)fieldsValue))
			.append("%'");

	}

	

	public void addConditionNotEqual(String fieldsName, Object fieldsValue,
			boolean NullSensitive) {
		addCondition( fieldsName, fieldsValue,"<>",NullSensitive);

	}


	public void addConditionSQLStr(String SQLStr) {
		SQLWhereBuffer.append(" AND ").append(SQLStr);
	}
	
	
	public void addConditionIn(String fieldsName, Object[] fieldsValue,
			String operation, boolean NullSensitive) {
			// null condition 
			if (nullProcessed(fieldsName, fieldsValue, NullSensitive))
				return;

			// not null
			SQLWhereBuffer.append(" AND ").append(fieldsName).append(" ").append(operation ).append( " (").append(getStringArrayValueFormated(fieldsValue)).append(")");
		
	}

	protected boolean nullProcessed(String fieldsName, Object fieldsValue, boolean NullSensitive) {
		if (needConverEmpty2Null) {
			fieldsValue=ConverSpace2Null(fieldsValue); // if needed
		}

		if (fieldsValue != null)
			return false;

		if (NullSensitive) {
			SQLWhereBuffer.append(" AND ").append(fieldsName).append(" IS NULL ");
			return true;
		} else {
			return true;
		}
	}

	private Object ConverSpace2Null(Object fieldValue) {
		
		if (fieldValue == null)
			return fieldValue;

		if ((fieldValue != null) && (fieldValue instanceof String )) {
			final String tempVal = ((String ) fieldValue).trim() ;
			
			if ("".equals(tempVal)) {
				
				return  null;
				
			}
		}
		return fieldValue;
	}
	protected static Object getStringValueFormated(Object value) {
		if (value != null) {
			value=Constance.filterHQL(String.valueOf(value));
			value="'"+value+"'";
			return value;
		}
		return value;
	}
		
	protected static Object getStringArrayValueFormated(Object[] value) {
		if (value != null) {
			Object newVal[] = new Object[value.length];
			for(int i=0;i<value.length;i++){
				newVal[i] = getStringValueFormated(value[i]);
			}
			return Constance.join(newVal,",");
		}
		return value;
	}
}
